package com.itsu.controller;

import com.github.pagehelper.PageInfo;
import com.itsu.domain.Catalog;
import com.itsu.domain.Course;
import com.itsu.service.CatalogService;
import com.itsu.service.CourseService;
import com.itsu.service.Impl.CatalogServiceImpl;
import com.itsu.service.Impl.CourseServiceImpl;
import com.itsu.utils.BeanUtil;
import com.mysql.cj.util.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.UUID;


@WebServlet("/store/catalog")
public class CatalogController extends HttpServlet {

    private CatalogService catalogService = new CatalogServiceImpl();

    private CourseService courseService = new CourseServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String operation = req.getParameter("operation");
        if ("list".equals(operation)){
            showList(req, resp);
        } else if ("toAdd".equals(operation)) {
            toAdd(req, resp);
        } else if ("save".equals(operation)) {
            save(req, resp);
        } else if ("toEdit".equals(operation)) {
            toEdit(req, resp);
        } else if ("edit".equals(operation)) {
            edit(req, resp);
        } else if ("delete".equals(operation)) {
            delete(req, resp);
        }
    }

    private void showList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int pageNum = 1;
        int pageSize = 10;
        if (!StringUtils.isNullOrEmpty(req.getParameter("page"))){
            pageNum = Integer.parseInt(req.getParameter("page"));
        }
        PageInfo<Catalog> pageInfo = catalogService.findAll(pageNum, pageSize);

        req.setAttribute("page", pageInfo);

        req.getRequestDispatcher("/WEB-INF/pages/store/catalog/list.jsp").forward(req, resp);
    }

    private void toAdd(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Course> courseList = courseService.findAll();
        req.setAttribute("courseList", courseList);
        req.getRequestDispatcher("/WEB-INF/pages/store/catalog/add.jsp").forward(req, resp);
    }

    private void save(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Catalog catalog = BeanUtil.fillBean(req, Catalog.class);
        catalog.setId(UUID.randomUUID().toString());
        catalog.setCreateTime(new Date());
        catalogService.save(catalog);

        resp.sendRedirect(req.getContextPath()+"/store/catalog?operation=list");
    }

    private void toEdit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        Catalog catalog = catalogService.findById(id);
        List<Course> courseList = courseService.findAll();

        req.setAttribute("courseList", courseList);
        req.setAttribute("catalog", catalog);
        req.getRequestDispatcher("/WEB-INF/pages/store/catalog/update.jsp").forward(req, resp);
    }

    private void edit(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Catalog catalog = BeanUtil.fillBean(req, Catalog.class);
        catalogService.update(catalog);

        resp.sendRedirect(req.getContextPath()+"/store/catalog?operation=list");
    }

    private void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Catalog catalog = BeanUtil.fillBean(req, Catalog.class);
        catalogService.delete(catalog);

        resp.sendRedirect(req.getContextPath()+"/store/catalog?operation=list");
    }
}
